using UnityEngine;
using UnityEngine.UI;
using TMPro;

public class ElegantUITheme : MonoBehaviour
{
    // 高级粉蓝配色
    public Color bannerBgColor = new Color(1f, 1f, 1f, 0.78f); // Banner半透明白
    public Color buttonBgColor = new Color(1f, 1f, 1f, 0.82f); // 按钮半透明白
    public Color borderColor = new Color(0.71f, 0.85f, 1f, 1f); // #b5d8ff 淡蓝边框
    public Color shadowColor = new Color(0.71f, 0.85f, 1f, 0.18f); // 淡蓝阴影
    public Color textColor = new Color(0f, 0f, 0f, 1f); // 纯黑
    public Color titleColor = new Color(0.82f, 0.67f, 1f, 1f); // 粉蓝色标题

    public Font elegantFont; // 可拖拽自定义字体

    [Header("渐变背景设置")]
    public Color gradientTop = new Color(0.97f, 0.81f, 1f, 1f); // 粉色
    public Color gradientBottom = new Color(0.71f, 0.85f, 1f, 1f); // 蓝色
    public float backgroundAlpha = 1f;

    private void Start()
    {
        // 添加全屏渐变背景
        AddOrUpdateGradientBackground();

        // 自动美化Banner和Button
        foreach (var img in GetComponentsInChildren<Image>(true))
        {
            string n = img.gameObject.name.ToLower();
            // Banner
            if (n.Contains("banner"))
            {
                img.color = bannerBgColor;
                AddOrUpdateOutline(img.gameObject, borderColor, 2.5f);
                AddOrUpdateShadow(img.gameObject, shadowColor, 2f, -2f);
                SetRoundness(img, 18);
            }
            // Button
            else if (n.Contains("button"))
            {
                img.color = buttonBgColor;
                AddOrUpdateOutline(img.gameObject, borderColor, 2f);
                AddOrUpdateShadow(img.gameObject, shadowColor, 1.5f, -1.5f);
                SetRoundness(img, 12);
            }
        }
        // 美化所有Button文字
        foreach (var btn in GetComponentsInChildren<Button>(true))
        {
            foreach (var txt in btn.GetComponentsInChildren<Text>(true))
            {
                txt.color = textColor;
                if (elegantFont) txt.font = elegantFont;
            }
            foreach (var tmp in btn.GetComponentsInChildren<TMP_Text>(true))
            {
                tmp.color = textColor;
                if (elegantFont) tmp.font = TMP_FontAsset.CreateFontAsset(elegantFont);
            }
        }
        // 美化所有Text
        foreach (var txt in GetComponentsInChildren<Text>(true))
        {
            if (txt.gameObject.name.ToLower().Contains("title") || txt.fontSize > 20)
                txt.color = titleColor;
            else
                txt.color = textColor;
            if (elegantFont) txt.font = elegantFont;
        }
        // 美化所有TMP_Text
        foreach (var tmp in GetComponentsInChildren<TMP_Text>(true))
        {
            if (tmp.gameObject.name.ToLower().Contains("title") || tmp.fontSize > 20)
                tmp.color = titleColor;
            else
                tmp.color = textColor;
            if (elegantFont) tmp.font = TMP_FontAsset.CreateFontAsset(elegantFont);
        }
    }

    private void AddOrUpdateGradientBackground()
    {
        var canvas = GetComponent<Canvas>();
        if (!canvas) { Debug.LogWarning("ElegantUITheme: 请挂载到Canvas对象上！"); return; }
        var bg = transform.Find("ElegantUI_GradientBG");
        Image bgImg;
        if (bg == null)
        {
            var go = new GameObject("ElegantUI_GradientBG", typeof(RectTransform), typeof(CanvasRenderer), typeof(Image));
            go.transform.SetParent(transform, false);
            go.transform.SetAsFirstSibling();
            bgImg = go.GetComponent<Image>();
            var rect = go.GetComponent<RectTransform>();
            rect.anchorMin = Vector2.zero;
            rect.anchorMax = Vector2.one;
            rect.offsetMin = Vector2.zero;
            rect.offsetMax = Vector2.zero;
        }
        else
        {
            bgImg = bg.GetComponent<Image>();
        }
        // 创建渐变材质
        bgImg.sprite = GenerateVerticalGradientSprite(gradientTop, gradientBottom, backgroundAlpha);
        bgImg.type = Image.Type.Sliced;
        bgImg.raycastTarget = false;
        bgImg.color = Color.white;
    }

    // 为UI对象添加或更新Outline组件
    private void AddOrUpdateOutline(GameObject go, Color color, float width)
    {
        var outline = go.GetComponent<Outline>();
        if (outline == null) outline = go.AddComponent<Outline>();
        outline.effectColor = color;
        outline.effectDistance = new Vector2(width, width);
        outline.useGraphicAlpha = true;
    }

    // 为UI对象添加或更新Shadow组件
    private void AddOrUpdateShadow(GameObject go, Color color, float x, float y)
    {
        var shadow = go.GetComponent<Shadow>();
        if (shadow == null) shadow = go.AddComponent<Shadow>();
        shadow.effectColor = color;
        shadow.effectDistance = new Vector2(x, y);
        shadow.useGraphicAlpha = true;
    }

    // 设置圆角（需配合9宫格Sprite或Mask，自动尝试）
    private void SetRoundness(Image img, float radius)
    {
        if (img.type == Image.Type.Sliced && img.sprite != null)
        {
            // 已有9宫格Sprite，自动适配
            return;
        }
        // 可选：可集成第三方圆角插件或自定义Shader
    }

    // 生成竖直渐变Sprite（兼容所有平台，无需Shader）
    private Sprite GenerateVerticalGradientSprite(Color top, Color bottom, float alpha)
    {
        int width = 4, height = 128;
        Texture2D tex = new Texture2D(width, height, TextureFormat.ARGB32, false);
        for (int y = 0; y < height; y++)
        {
            Color c = Color.Lerp(bottom, top, y / (float)(height - 1));
            c.a *= alpha;
            for (int x = 0; x < width; x++)
                tex.SetPixel(x, y, c);
        }
        tex.Apply();
        tex.wrapMode = TextureWrapMode.Clamp;
        var sprite = Sprite.Create(tex, new Rect(0, 0, width, height), new Vector2(0.5f, 0.5f));
        return sprite;
    }
} 